package org.javacourse.service;

import org.javacourse.entity.Customer;
import org.javacourse.entity.Product;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tania.luchin
 * Date: 4/9/13
 * Time: 2:06 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProductService extends AbstractService {

    public Product getProductById(long id){
        Query q = em.createQuery("SELECT pr FROM Product pr WHERE pr.id = :id", Product.class);
        q.setParameter("id", id);
        return (Product) q.getSingleResult();
    }



    public Product deleteProductById(long id){
        Query q = em.createQuery("DELETE FROM Product pr WHERE pr.id = :id", Product.class);
        q.setParameter("id", id);
        return (Product) q.getSingleResult();
    }


    public List<Product> getAllProducts(){
        Query q = em.createQuery("SELECT pr FROM Product pr" , Product.class);
        return (List<Product>) q.getResultList();
    }



}
